package com.bw.gmall.realtime.dws.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.realtime.common.base.BaseApp;
import com.bw.gmall.realtime.common.bean.TradeProvinceOrderBean;
import com.bw.gmall.realtime.common.constant.Constant;
import com.bw.gmall.realtime.common.util.DateFormatUtil;
import com.bw.gmall.realtime.common.util.FlinkSinkUtil;
import com.bw.gmall.realtime.common.util.HbaseUtil;
import com.bw.gmall.realtime.dws.function.DorisMapFunction;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.hadoop.hbase.client.Connection;

import java.math.BigDecimal;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashSet;

/*
12.10交易域省份粒度下单各窗口汇总表
 */

public class DwsTradeProvinceOrderWindow extends BaseApp {
    public static void main(String[] args) {
        new DwsTradeProvinceOrderWindow().start(Constant.TOPIC_DWD_TRADE_ORDER_DETAIL,
                Constant.DWS_TRADE_PROVINCE_ORDER_WINDOW,4,10031);
    }

    @Override
    public void handle(StreamExecutionEnvironment env, DataStreamSource<String> dataStreamSource) {
        // 1.etl清洗数据
        SingleOutputStreamOperator<JSONObject> etlStream = etl(dataStreamSource);

        // 2.设置水位线
        SingleOutputStreamOperator<JSONObject> watermarksStream = getWatermarksStream(etlStream);

        // 3.同一笔详情订单分组,计算对应指标(回撤流)
        SingleOutputStreamOperator<TradeProvinceOrderBean> processStream = getProcessStream(watermarksStream);

        // 4.按照省份分组聚合
        SingleOutputStreamOperator<TradeProvinceOrderBean> reduceStream = getReduceStream(processStream);
//        reduceStream.print();

        // 5.同步关联
        SingleOutputStreamOperator<TradeProvinceOrderBean> result = getResult(reduceStream);
        result.print();

        // 6.写到doris
        result.map(new DorisMapFunction<>())
                .sinkTo(FlinkSinkUtil.getDorisSink(Constant.DWS_TRADE_PROVINCE_ORDER_WINDOW));
    }

    private static SingleOutputStreamOperator<TradeProvinceOrderBean> getResult(SingleOutputStreamOperator<TradeProvinceOrderBean> reduceStream) {
        return reduceStream.map(new RichMapFunction<TradeProvinceOrderBean, TradeProvinceOrderBean>() {
            Connection hbaseConnect;

            @Override
            public void open(Configuration parameters) throws Exception {
                hbaseConnect = HbaseUtil.getHbaseConnect();
            }

            @Override
            public void close() throws Exception {
                hbaseConnect.close();
            }

            @Override
            public TradeProvinceOrderBean map(TradeProvinceOrderBean tradeProvinceOrderBean) throws Exception {
                JSONObject dimBaseProvince = HbaseUtil.getCells(hbaseConnect, Constant.HBASE_NAMESPACE, "dim_base_province", tradeProvinceOrderBean.getProvinceId());
                tradeProvinceOrderBean.setProvinceName(dimBaseProvince.getString("name"));
                return tradeProvinceOrderBean;
            }
        });
    }

    private static SingleOutputStreamOperator<TradeProvinceOrderBean> getReduceStream(SingleOutputStreamOperator<TradeProvinceOrderBean> processStream) {
        return processStream.keyBy(x -> x.getProvinceId())
                .window(TumblingEventTimeWindows.of(org.apache.flink.streaming.api.windowing.time.Time.seconds(5)))
                .reduce(new ReduceFunction<TradeProvinceOrderBean>() {
                    @Override
                    public TradeProvinceOrderBean reduce(TradeProvinceOrderBean x, TradeProvinceOrderBean y) throws Exception {
                        x.setOrderAmount(x.getOrderAmount().add(y.getOrderAmount()));
                        x.setOrderCount(x.getOrderCount() + y.getOrderCount());
                        x.getOrderIdSet().addAll(y.getOrderIdSet());
                        x.setOrderCount((long) x.getOrderIdSet().size());
                        return x;
                    }
                }, new ProcessWindowFunction<TradeProvinceOrderBean, TradeProvinceOrderBean, String, TimeWindow>() {
                    @Override
                    public void process(String s, ProcessWindowFunction<TradeProvinceOrderBean, TradeProvinceOrderBean, String, TimeWindow>.Context context, Iterable<TradeProvinceOrderBean> iterable, Collector<TradeProvinceOrderBean> collector) throws Exception {
                        TradeProvinceOrderBean tradeProvinceOrderBean = iterable.iterator().next();
                        tradeProvinceOrderBean.setStt(DateFormatUtil.tsToDateTime(context.window().getStart()));
                        tradeProvinceOrderBean.setEdt(DateFormatUtil.tsToDateTime(context.window().getEnd()));
                        tradeProvinceOrderBean.setCurDate(DateFormatUtil.tsToDate(System.currentTimeMillis()));

                        collector.collect(tradeProvinceOrderBean);
                    }
                });
    }

    private static SingleOutputStreamOperator<TradeProvinceOrderBean> getProcessStream(SingleOutputStreamOperator<JSONObject> watermarksStream) {
        return watermarksStream.keyBy(x -> x.getString("id"))
                .process(new KeyedProcessFunction<String, JSONObject, TradeProvinceOrderBean>() {
                    MapState<String, BigDecimal> lastOrderState;

                    @Override
                    public void open(Configuration parameters) {
                        MapStateDescriptor<String, BigDecimal> stateDescriptor = new MapStateDescriptor<>("state",
                                String.class, BigDecimal.class);
                        //设置状态存储过期时间ttl
                        stateDescriptor.enableTimeToLive(StateTtlConfig.newBuilder(Time.seconds(30)).build());
                        lastOrderState = getRuntimeContext().getMapState(stateDescriptor);
                    }

                    @Override
                    public void processElement(JSONObject jsonObject, KeyedProcessFunction<String, JSONObject, TradeProvinceOrderBean>.Context context, Collector<TradeProvinceOrderBean> collector) throws Exception {
                        //取到状态指标，并判断
                        BigDecimal orderAmount = lastOrderState.get("orderAmount");
                        orderAmount = orderAmount == null ? new BigDecimal(0) : orderAmount;
                        //数据取值
                        BigDecimal newOrderAmount = jsonObject.getBigDecimal("split_total_amount");
                        //创建hashset,存储orderid,用于去重计算下单次数
                        HashSet<String> set = new HashSet<>();
                        set.add(jsonObject.getString("order_id"));
                        //给到实体类
                        collector.collect(TradeProvinceOrderBean.builder()
                                .provinceId(jsonObject.getString("province_id"))
                                .orderCount((long) set.size())
                                .orderAmount(newOrderAmount.subtract(orderAmount))
                                .ts(jsonObject.getLong("ts"))
                                .orderDetailId(jsonObject.getString("id"))
                                .orderIdSet(set)
                                .build());
                        lastOrderState.put("orderAmount",newOrderAmount);
                    }
                });
    }

    private static SingleOutputStreamOperator<JSONObject> getWatermarksStream(SingleOutputStreamOperator<JSONObject> etlStream) {
        return etlStream.assignTimestampsAndWatermarks(WatermarkStrategy
                .<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                .withTimestampAssigner((event, timestamp) -> event.getLong("ts"))
                .withIdleness(Duration.ofSeconds(120)));
    }

    private static SingleOutputStreamOperator<JSONObject> etl(DataStreamSource<String> dataStreamSource) {
        return dataStreamSource.flatMap(new FlatMapFunction<String, JSONObject>() {
            @Override
            public void flatMap(String s, Collector<JSONObject> collector) throws Exception {
                try {
                    if (s != null){
                        JSONObject jsonObject = JSON.parseObject(s);
                        String provinceId = jsonObject.getString("province_id");
                        Long ts = jsonObject.getLong("ts");
                        if (provinceId != null && ts != null) {
                            jsonObject.put("ts", ts * 1000);
                            collector.collect(jsonObject);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
